import React from "react";
import Index from "../view/index";
import Topic from "../view/topic";
import User from "../view/user";
import GetStart from "../view/getStart";
import About from "../view/about";
import UndefinedPage from "../view/page404";
import qs from "qs";

const types = ["all", "good", "share", "ask", "job", "dev"];

const route = [
    {
        path: "/",
        exact: true,
        render(props) {
            let { location } = props;
            let { search } = location;
            let { page, tab } = qs.parse(search.substr(1));
            if (
                (tab === undefined && page === undefined) ||
                (types.includes(tab) && (page === undefined || page > 0))
            ) {
                return <Index {...props} />;
            } else {
                return <UndefinedPage {...props} />;
            }
        },
    },
    {
        path: "/topics/:id",
        exact: true,
        render(props) {
            return <Topic {...props} />;
        },
    },
    {
        path: "/user/:loginname",
        exact: true,
        render(props) {
            return <User {...props} />;
        },
    },
    {
        path: "/getStart",
        exact: true,
        render(props) {
            return <GetStart {...props} />;
        },
    },
    {
        path: "/about",
        exact: true,
        render(props) {
            return <About {...props} />;
        },
    },
    {
        path: "",
        exact: false,
        render(props) {
            return <UndefinedPage {...props} />;
        },
    },
];

const nav = [
    {
        to: "/",
        txt: "首页",
    },
    {
        to: "/getStart",
        txt: "新手入门",
    },
    {
        to: "/about",
        txt: "关于",
    },
];

const indexNav = [
    {
        name: "全部",
        url: "/?tab=all",
    },
    {
        name: "精华",
        url: "/?tab=good",
    },
    {
        name: "分享",
        url: "/?tab=share",
    },
    {
        name: "问答",
        url: "/?tab=ask",
    },
    {
        name: "招聘",
        url: "/?tab=job",
    },
    {
        name: "客户端测试",
        url: "/?tab=dev",
    },
];
export { route, nav, indexNav, types };
